package com.zuqiukong.automaticpacking.action;

import com.google.gson.Gson;
import com.zuqiukong.automaticpacking.Constants;
import com.zuqiukong.automaticpacking.model.Model;
import com.zuqiukong.automaticpacking.pojo.ChannelPojo;
import com.zuqiukong.automaticpacking.pojo.ResponseBasePojo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Servlet implementation class QueryLog
 */
@WebServlet("/queryTag")
public class QueryTag extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private Gson gson = new Gson();

    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryTag() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        ResponseBasePojo<ChannelPojo> responsePojo = new ResponseBasePojo<>();
        responsePojo = new ResponseBasePojo<ChannelPojo>();
        responsePojo.ret_code = 1;
        responsePojo.ret_msg = getTagLists();
        responsePojo.ret_error_msg = Constants.packingBetaErrorMsg;
        response.getWriter().append(gson.toJson(responsePojo));
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }


    private String getTagLists() {
        /*
         * 查看最近10次提交中，带‘betaMag:’字样的提交说明信息
		 * git log -10 --pretty=format:"%s" | grep 'betaMsg:'
		 */
        String[] cmdLog = {"cmd", "/c", "git", "-C", Constants.PROJECT_CHECK_VERSION_PATH, "tag", "-l"};
        StringBuilder tags = new StringBuilder();
        Process pro = null;
        try {
            pro = Runtime.getRuntime().exec(cmdLog);
            pro.waitFor();
            InputStream in = pro.getInputStream();
            InputStreamReader isr = new InputStreamReader(in, "UTF-8");
            BufferedReader read = new BufferedReader(isr);
            String line = null;
            ArrayList<String> array = new ArrayList<String>();
            while ((line = read.readLine()) != null) {
                array.add(line);
            }
            for (int i = array.size(); i > 0; i--) {
                tags.append("<option>" + array.get(i - 1) + "</option>");
            }
            pro.destroy();
            read.close();
            in.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != pro)
                pro.destroy();
        }
        return tags.toString().replace("betaMsg:", "");
    }
}
